//
//  TitleViewSegViewController.m
//  TestNav4
//
//  Created by wp w on 12-6-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TitleViewSegViewController.h"


#define COOKBOOK_PURPLE_COLOR	[UIColor colorWithRed:0.80392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) 	[[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]

@interface TitleViewSegViewController ()

@end

@implementation TitleViewSegViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = @"titleView - UISegmentedControl";
    
    //设置bar背景色
    self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
	
	// Create the segmented control. Choose one of the three styles
	NSArray *buttonNames = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", @"Six", nil];
	UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
	[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
	segmentedControl.momentary = YES;   //选中状态，是否时鼠标离开后消失
	
    
	// Add it to the navigation bar
	self.navigationItem.titleView = segmentedControl;
	[segmentedControl release];
    
    //显示当前选中的index
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(101, 43, 118, 118)];
    [label setTag:101];
    label.font = [UIFont systemFontOfSize:100];
    [self.view addSubview:label];
    [label release];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10, 200, 200, 30)];
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"hide/show back btn" forState:UIControlStateNormal];
//    btn.titleLabel.text = @"hide/show back btn";
    [btn addTarget:self action:@selector(hideOrShowBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn release];
}

-(void) segmentAction: (UISegmentedControl *) sender
{
	// Update the label with the segment number
	UILabel *label = (UILabel *)[self.view viewWithTag:101];
	[label setText:[NSString stringWithFormat:@"%0d", sender.selectedSegmentIndex + 1]];
}

- (void)hideOrShowBack
{
    self.navigationItem.hidesBackButton = !self.navigationItem.hidesBackButton;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
